"""
配置管理模块
"""
import os
from typing import List
from dotenv import load_dotenv
from pydantic_settings import BaseSettings

# 加载环境变量
load_dotenv("config.env")  # 明确指定配置文件路径

class Settings(BaseSettings):
    """应用配置"""
    
    # 数据库配置
    DB_HOST: str = os.getenv("DB_HOST", "localhost")
    DB_PORT: int = int(os.getenv("DB_PORT", "3306"))
    DB_USER: str = os.getenv("DB_USER", "root")
    DB_PASSWORD: str = os.getenv("DB_PASSWORD", "")  # 必须通过环境变量配置
    DB_NAME: str = os.getenv("DB_NAME", "api_test_db")
    
    # 认证配置
    AUTH_URL: str = os.getenv("AUTH_URL", "https://ae-openapi.feishu.cn/auth/v1/appToken")
    AUTH_CLIENT_ID: str = os.getenv("AUTH_CLIENT_ID", "")  # 必须通过环境变量配置
    AUTH_CLIENT_SECRET: str = os.getenv("AUTH_CLIENT_SECRET", "")  # 必须通过环境变量配置
    
    # 元数据接口配置
    METADATA_BASE_URL: str = os.getenv("METADATA_BASE_URL", "https://ae-openapi.feishu.cn/api/data/v1/namespaces/package_d6426c__c/meta/objects")
    NAMESPACE: str = os.getenv("NAMESPACE", "package_d6426c__c")
    
    # AI配置
    AI_MODEL_TYPE: str = os.getenv("AI_MODEL_TYPE", "openai")
    
    # OpenAI API配置
    OPENAI_API_KEY: str = os.getenv("OPENAI_API_KEY", "")
    OPENAI_MODEL: str = os.getenv("OPENAI_MODEL", "gpt-3.5-turbo")
    
    # 通义千问API配置
    QWEN_API_KEY: str = os.getenv("QWEN_API_KEY", "")
    QWEN_MODEL: str = os.getenv("QWEN_MODEL", "qwen-plus")
    
    # 智谱AI API配置
    ZHIPU_API_KEY: str = os.getenv("ZHIPU_API_KEY", "")
    ZHIPU_MODEL: str = os.getenv("ZHIPU_MODEL", "glm-4-flash")
    
    # 日志配置
    LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
    LOG_FILE: str = os.getenv("LOG_FILE", "logs/app.log")
    
    # 测试配置
    MAX_RETRY_COUNT: int = int(os.getenv("MAX_RETRY_COUNT", "3"))
    REQUEST_TIMEOUT: int = int(os.getenv("REQUEST_TIMEOUT", "30"))

    # CURD接口配置
    CURD_BASE_URL: str = os.getenv("CURD_BASE_URL", "https://ae-openapi.feishu.cn/v1/data/namespaces/package_d6426c__c/objects/")
    
    @property
    def database_url(self) -> str:
        """获取数据库连接URL"""
        return f"mysql+pymysql://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}"
    
    def validate_config(self) -> List[str]:
        """
        验证必要的配置是否已提供
        
        Returns:
            缺失的配置项列表
        """
        missing_configs = []
        
        # 检查必要的配置项
        required_configs = {
            "DB_PASSWORD": self.DB_PASSWORD,
            "AUTH_CLIENT_ID": self.AUTH_CLIENT_ID,
            "AUTH_CLIENT_SECRET": self.AUTH_CLIENT_SECRET,
        }
        
        for config_name, config_value in required_configs.items():
            if not config_value or config_value.strip() == "":
                missing_configs.append(config_name)
        
        return missing_configs
    
    def check_config_or_exit(self):
        """检查配置，如果有缺失则退出程序"""
        missing = self.validate_config()
        if missing:
            print("[错误] 以下必要配置缺失，请在config.env文件中配置：")
            for config in missing:
                print(f"   - {config}")
            print("\n请参考config.env.template文件创建config.env配置文件")
            exit(1)

# 全局配置实例
settings = Settings() 